﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class WGGINV : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI
        {
            get
            {
                return (INTEXDataSet)TryFindResource("iNTEXDataSet");
            }
        }
        public INTEXDataSet setWorker=new INTEXDataSet();
        WGGINVData m_Data, m_DataX;
        public DataSet setResult;
        
        private DataRow dwUI;
        WGGINVReport m_rpt;
        bool flagRegenerateLastMonthData;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataSet setANINFOR = new DataSet();
        DataTable tableUI;
        string LASTINVDATE;
        public WGGINV()
        {
            InitializeComponent();
            this.Name = "产品进出流水帐";
            this.Title = "产品进出流水帐";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
               

                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
                m_Data = new WGGINVData();
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;
                tableUI = new DataTable("UIData");
                tableUI.Columns.Add("INVDATE", typeof(string));
                tableUI.Columns.Add("LASTINVDATE", typeof(string));
                
                tableUI.Columns.Add("WBSRTDATE", typeof(string));
                tableUI.Columns.Add("WBENTDATE", typeof(string));
                txtMONTH.Text = m_Data.Right("00" + DateTime.Now.Month.ToString(), 2);
                txtYEAR.Text = DateTime.Now.Year.ToString();
                flagRegenerateLastMonthData = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                setUI.AcceptChanges();
                string InvYear = txtYEAR.Text.ToString();
                string InvMonth = m_Data.Right("00" + txtMONTH.Text.ToString(), 2);
                string InvDay = m_Data.Right("00" + txtDAY.Text.ToString(), 2);
                InvDay = "01";
                int eYear = System.Convert.ToInt16(InvYear);
                int eMonth = System.Convert.ToInt16(InvMonth);
                int eDay = System.Convert.ToInt16(InvDay);
                int lastMonth = System.Convert.ToInt16(InvMonth) - 1;
                int lastYear = System.Convert.ToInt16(InvYear);
                if (lastMonth == 0)
                {
                    lastYear = lastYear - 1;
                }
                string WBSRTDATE = InvYear
                    + m_Data.Right("00" + InvMonth, 2)
                    + m_Data.Right("00" + InvDay.ToString(), 2);
                eDay = DateTime.DaysInMonth(eYear, eMonth);
                string WBENTDATE = eYear.ToString()
                    + m_Data.Right("00" + eMonth.ToString(), 2)
                    + m_Data.Right("00" + eDay.ToString(), 2);
                string INVDATE = InvYear + InvMonth;
                string nYear = InvYear;
                string nMonth = m_Data.Right("00" + (System.Convert.ToInt16(InvMonth) - 1).ToString(), 2);
                if (nMonth == "00")
                {
                    nMonth = "12";
                    nYear = m_Data.Right("0000" + (System.Convert.ToInt16(InvYear) - 1).ToString(), 4);
                }
                LASTINVDATE = nYear + nMonth;
                if (flagRegenerateLastMonthData == true)
                {
                    WBENTDATE = DateTime.Now.Year
                        + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                        + m_Data.Right("00" + DateTime.Now.Day, 2);
                }
                dwUI["INVDATE"] = INVDATE;
                dwUI["LASTINVDATE"] = LASTINVDATE;
                dwUI["WBSRTDATE"] = WBSRTDATE;
                dwUI["WBENTDATE"] = WBENTDATE;
                m_Data.flagRegenerateLastMonthData = flagRegenerateLastMonthData;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    setResult = m_Data.BuildWBBST(dwUI);
                    setUI.Merge(setResult);
                    totalrecord = setUI.ABPOS.Rows.Count;
                    MasterDg.DataContext = setUI.Tables["WBBEW"];
                    this.SndDg.DataContext = setUI.Tables["WBDATINV"];
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dateChooser = (DatePicker)this.FindName("DateChooser");
                
                dwUI = tableUI.NewRow();                
                if (setUI != null)
                {
                    //setResult = m_Data.GetWASTAMM();
                    //setUI.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        } 
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;  
                     
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Date_GotFocus(object sender, RoutedEventArgs e)
        {
             
        } 
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        public void GenerateReport(string PrintType)
        {
            Process[] AllProcesses = Process.GetProcessesByName("EXCEL");

            // check to kill the right process
            /*foreach (Process ExcelProcess in AllProcesses)
            {
                ExcelProcess.Kill();
            }*/
            using (new LongOperation(this, "读取符合条件的记录"))
            { 
                m_DataX = new WGGINVData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                m_rpt = new WGGINVReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
        }
        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            //tb.Text = "";
        }

        private void btnRegenerateLastInv_Click(object sender, RoutedEventArgs e)
        {
            flagRegenerateLastMonthData = true;
            btnQuery_Click(null, null);
            flagRegenerateLastMonthData = false;
        }

        private void btnSaveAsInv_Click(object sender, RoutedEventArgs e)
        {
            m_Data.SaveAsInv();

        }
    }
}
